const plugin = require('../../index');
function unique(arr){
  //Set数据结构，它类似于数组，其成员的值都是唯一的
  return Array.from(new Set(arr)); // 利用Array.from将Set结构转换成数组
 }
Page({

  /**
   * 页面的初始数据
   */
  data: {
    word: '',
    wordList: [],
    cateId: '', //单词册唯一标识
    cateIndex: 0,
    userId: 'kindear'
  },
  preTap(e) {
    console.log('上一个')
    let index = this.data.cateIndex
    let cateIndex = index - 1;
    if (cateIndex == -1) {
      wx.showToast({
        title: '这是第一个单词',
        icon: 'none'
      })
    } else {
      let cateId = this.data.cateId
      wx.setStorageSync(cateId + '_Index', cateIndex)
      this.setData({
        cateIndex,
        word: this.data.wordList[cateIndex]
      }, () => {
        this.checkFavor()
        let todayReadWord = wx.getStorageSync(plugin.TimeCodeYmd()+'_ReadWord') || []
        todayReadWord.push(this.data.word)
        todayReadWord = unique(todayReadWord)
        wx.setStorageSync(plugin.TimeCodeYmd()+'_ReadWord', todayReadWord)
      })
    }

  },
  nextTap() {
    console.log('下一个')
    let index = this.data.cateIndex
    let cateIndex = index + 1;
    if (cateIndex == this.data.wordList.length) {
      wx.showToast({
        title: '已经结束了',
        icon: 'none'
      })
    } else {
      let cateId = this.data.cateId
      wx.setStorageSync(cateId + '_Index', cateIndex)
      this.setData({
        cateIndex,
        word: this.data.wordList[cateIndex]
      }, () => {
        this.checkFavor()
        let todayReadWord = wx.getStorageSync(plugin.TimeCodeYmd()+'_ReadWord') || []
        // console.log(todayReadWord)
        todayReadWord.push(this.data.word)
        todayReadWord = unique(todayReadWord)
        wx.setStorageSync(plugin.TimeCodeYmd()+'_ReadWord', todayReadWord)
      })
    }
  },
  favorTap() {
    if (this.data.isFavor) {
      //取消收藏
      plugin.pullFavor(this.data.userId, this.data.word).then(res => {
        console.log(res)
        let msg = '取消收藏成功'
        let isFavor = false
        if (res.errCode == -1) {
          msg = res.errMsg
          isFavor = true
        }
        wx.showToast({
          title: msg,
          icon: 'none'
        })
        this.setData({
          isFavor
        })
      })
    } else {
      plugin.addFavor(this.data.userId, this.data.word).then(res => {
        console.log(res)
        let msg = '收藏成功'
        let isFavor = true
        if (res.errCode == -1) {
          msg = res.errMsg
          isFavor = false
        }
        wx.showToast({
          title: msg,
          icon: 'none'
        })
        this.setData({
          isFavor
        })
      })
    }

  },
  checkFavor() {
    plugin.isFavor(this.data.userId, this.data.word).then(res => {
      console.log(res)
      this.setData({
        isFavor: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let word = options.word
    let userId = options.userId
    this.setData({
      userId
    })
    var that = this;
    let cateId = ''
    // console.log(word)
    if (word == undefined || word == 'undefined') {
      cateId = options.cateId
      let wordList = wx.getStorageSync('wordList') || []
      // 获取上次坐标
      let cateIndex = wx.getStorageSync(cateId + '_Index') || 0
      if (cateIndex != 0) {
        wx.showToast({
          title: '重定位到上次结束位置',
          icon: 'none'
        })
      }
      if (cateIndex >= wordList.length) {
        cateIndex = parseInt(wordList.length - 1)
      }
      console.log(cateIndex)
      // console.log(wordList)
      this.setData({
        cateId,
        wordList,
        cateIndex,
        word: wordList[cateIndex]
      }, () => {
        //查询是否被收藏
        that.checkFavor();
      })
    } else {
      this.setData({
        word
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})